/**
 * 
 */
package com.apache.website.service.plugins;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.member.entity.Member;
import com.apache.member.entity.MemberRole;
import com.apache.member.manager.MemberManager;
import com.apache.member.manager.MemberRoleManager;

/**
 * @author dyhou
 *
 */
public class MemberEntityPluginImpl implements PluginConnector {

	MemberManager memberManager;
	MemberRoleManager memberRoleManager;

	/* (non-Javadoc)
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
	 */
	public Object execute(ParamsVo param) throws Exception {
		String memberId = String.valueOf(param.getParams("memberId"));
		Map<String, Object> resObj = new HashMap<String, Object>();
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(memberId)) {
			entity.setResult("false");
			entity.setMessage("缺少必要参数");
			return entity;
		}
		ParamsVo<Member> cvo = new ParamsVo<Member>();
		Member member = new Member();
		List<MemberRole> memberRoleList = null;
		if (Validator.isNotNull(memberId)) {
			cvo.setInfoId(memberId);
			member = (Member) memberManager.getInfoById(cvo);
			//同时将该会员所拥有的角色也查询出来。放到memberRoles中。
			if (Validator.isEmpty(member)) {
				entity.setResult("false");
				entity.setMessage("不是会员");
				return entity;
			}
			Map<String, Object> mapData = new HashMap<String, Object>();
			ParamsVo<MemberRole> vo = new ParamsVo<MemberRole>();
			MemberRole memberRole = new MemberRole();
			memberRole.setMemberId(memberId);
			vo.setObj(memberRole);
			memberRoleList = memberRoleManager.getList(vo);
			String memberRoles = "";
			if (memberRoleList.size() > 0) {
				for (MemberRole role : memberRoleList) {
					if (Validator.isNull(memberRoles)) {
						memberRoles = role.getRoleEname();
					} else {

						memberRoles = memberRoles + "," + role.getRoleEname();
					}
				}
			}
			member.setMemberRoles(memberRoles);

		}
		resObj.put("member", member);
		resObj.put("memberRoles", memberRoleList);

		entity.setResult("true");
		entity.setEntity(resObj);
		entity.setMessage("查询成功");
		return entity;
	}

	public void setMemberManager(MemberManager memberManager) {
		this.memberManager = memberManager;
	}

	public void setMemberRoleManager(MemberRoleManager memberRoleManager) {
		this.memberRoleManager = memberRoleManager;
	}

}
